package com.molamil.outboard.shortcut {
	import flash.ui.Keyboard;
	import com.molamil.outboard.Outboard;
	import com.molamil.outboard.IShortcut;
	import com.molamil.outboard.MidiService;

	/**
	 * @author martindyrby
	 */
	public class MidiPositionShortcut implements IShortcut {
		
		public var controlIDX:uint;
		public var controlIDY:uint;
		public var target:*;
		public var name:String;
		public var precision : uint;
		public var midiService : MidiService;
		public var xOffset : Number;
		public var yOffset : Number;
				
		private var xIndex:int;
		private var yIndex:int;
		private var outboard:Outboard;


		
		public function init(outb:Outboard) : void {
			outboard = outb;
			
			xIndex = midiService.onMidiControl(controlIDX, onX);
			yIndex = midiService.onMidiControl(controlIDY, onY);
		}
		
		public function reactivate() : void {
			outboard.reactivateListener(xIndex);
			outboard.reactivateListener(yIndex);
		}

		public function deactivate() : void {
			outboard.deactivateListener(xIndex);
			outboard.deactivateListener(yIndex);
		}

		private function onX(value:Number) : void {
			value = (precision > 0) ? value * (127/(precision*10)) : value * 127;
			target.x = value + xOffset;
			trace(name, "x", target.x);
		}

		private function onY(value:Number) : void {
			value = (precision > 0) ? value * (127/(precision*10)) : value * 127;
			target.y = value + yOffset;
			trace(name, "y", target.y);
		}
	}
}
